package com.itheima.LockTest;

import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @PackageName: com.itheima.exampleAop
 * @ClassName: MylockClass
 * @Author: Renyz
 * @Date: 2021/11/11 15:25
 * @Description: //TODO
 */


//实现自旋锁
@Component
public class SpinLock {
    static AtomicReference<Thread> atomicReference = new AtomicReference<>();
    //上锁
    public static void mylock(){
        Thread thread = Thread.currentThread();
        String name = thread.getName();
        while (!atomicReference.compareAndSet(null, thread)){
        }

    }
    //解锁
    public static void unMylock(){
        Thread thread = Thread.currentThread();
        String name = thread.getName();
        atomicReference.compareAndSet(thread, null);
    }
}
